// =============================================================
// CSpace test
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Sep. 4, 2007
// =============================================================
#include <iostream>

#include "CSpace.h"
#include "CMessage.h"

int main(int argc, char **argv)
{
	CSpace space("lookup test");
	CMessage msg1, msg2, msg3;

	msg1.set("this is first");
	msg2.set("this is second");
	msg3.set("this is third");

	cout << "put message : " << msg1.get() << endl;
	cout << "current size = " << space.write(&msg1, 20) << endl;
	cout << "put message : " << msg2.get() << endl;
	cout << "current size = " << space.write(&msg2, 10) << endl;

vector< CSmartTemplate<CObject> > all;
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "lookup test for duplicated messages" << endl;
	cout << "-----------------------------------" << endl;
	cout << "put message : " << msg1.get() << endl;
	cout << "current size = " << space.write(&msg1) << endl;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << "#### Attention Begin####" << endl;
	cout << "put \"" << msg3.get() << "\" into \"" << msg1.get() << "\"" << endl;
	cout << "current size = " << space.write(&msg1, &msg3) << endl;
	cout << "#### Attention End####" << endl;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << endl;
	cout << "lookup test for read by key" << endl;
	cout << "-----------------------------------" << endl;
	cout << "get message : " << msg2.get() << endl;
	CMessage *res1 = NULL;
	res1 = static_cast<CMessage *>(space.read(&msg2,res1));
	//res1 = static_cast<CMessage *>(space.read(res1));
	cout << "obtained messsage : " << res1->get() << endl;
	cout << "current isze = " << space.size() << endl;
	delete res1;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	cout << endl;
	cout << "lookup test for take by key" << endl;
	cout << "-----------------------------------" << endl;
	cout << "take message : " << msg3.get() << endl;
	vector< CSmartTemplate<CObject> > all_res;
	//vector<CMessage> all_res;
	all_res = space.take(&msg3, space.size(), all_res);
	for(int i=0;i<all_res.size();i++) {
		cout << "taken message[" << i << "] : " << static_cast<CMessage &>(*(all_res[i])).get() << endl;
		//cout << "taken message[" << i << "] : " << all_res[i].get() << endl;
	}
	if(all_res.size()<1) cout << "there is no message : " << msg3.get() << endl;
	cout << "current isze = " << space.size() << endl;

all.clear();
all = space.read(NULL,space.size(), all);
cout << "-------------- all objects ---------------" << endl;
for(int i=0;i<all.size();i++) static_cast<CMessage &>(*(all[i])).print();
cout << "-------------------------- ---------------" << endl << endl;

	return 1;
}
